package poker;
import java.awt.image.ConvolveOp;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import poker.ai.MediumAI;
import poker.ai.OpponentModeler;
import poker.ai.SmartAI;
import poker.ai.StupidAI;
import poker.gui.GUI_Table;


public class PokerManager extends JFrame {

	private static final long serialVersionUID = 1L;

	int numberOfRoundsBeforeGUI;
	int totalPlayersCounter;

	int totalPlayers;
	int AIPlayers;

	int liberalStupidAIPlayers;
	int conservativeStupidAIPlayers;

	int liberalMediumAIPlayers;
	int conservativeMediumAIPlayers;
	
	int liberalSmartAIPlayers;
	int conservativeSmartAIPlayers;
	
	int humanPlayers;
	
	GameRound currentRound;
	ArrayList<GameRound> gameRounds;
	ArrayList<Player> players;

	GUI_Table gui;

	private ArrayList<Player> createPlayers(int total, int humans)
	{
		players = new ArrayList<Player>();
		totalPlayersCounter = totalPlayers;

		while(totalPlayersCounter > 0){
			for (int i = 0; i < humans; i++) {
				players.add(new Human(1500, "Human " + (i+1)));
				totalPlayersCounter--;
			}

			for(int i = 0; i < liberalStupidAIPlayers; i++)
			{
				players.add(new StupidAI(1500, "Liberal Stupid AI " + (i+1), false));
				totalPlayersCounter--;
			}			

			for(int i = 0; i < conservativeStupidAIPlayers; i++)
			{
				players.add(new StupidAI(1500, "Conserv. Stupid AI " + (i+1), true));
				totalPlayersCounter--;
			}	

			for(int i = 0; i < liberalMediumAIPlayers; i++)
			{
				players.add(new MediumAI(1500, "Liberal Medium AI " + (i+1), false));
				totalPlayersCounter--;
			}
			
			for(int i = 0; i < conservativeMediumAIPlayers; i++)
			{
				players.add(new MediumAI(1500, "Conserv. Medium AI " + (i+1), true));
				totalPlayersCounter--;
			}
			
			for(int i = 0; i < liberalSmartAIPlayers; i++)
			{
				players.add(new SmartAI(1500, "Liberal Smart AI " + (i+1), false));
				totalPlayersCounter--;
			}
			
			for(int i = 0; i < conservativeSmartAIPlayers; i++)
			{
				players.add(new SmartAI(1500, "Conserv. Smart AI " + (i+1), true));
				totalPlayersCounter--;
			}
		}
		return players;
	}

	void startNewGame()
	{
		numberOfRoundsBeforeGUI = 0;
		totalPlayers = -1;
		humanPlayers = -1;

		liberalStupidAIPlayers = -1;
		conservativeStupidAIPlayers = -1;

		liberalMediumAIPlayers = -1;
		conservativeMediumAIPlayers = -1;

		liberalSmartAIPlayers = -1;
		conservativeSmartAIPlayers = -1;
		
		String nrRoundBefGUI = JOptionPane.showInputDialog("Please specify the number of rounds before GUI:", numberOfRoundsBeforeGUI);	
		numberOfRoundsBeforeGUI = Integer.parseInt(nrRoundBefGUI);

		while(totalPlayers < 2 || totalPlayers > 10)
		{
			try
			{
				String temp = JOptionPane.showInputDialog("Please specify the number of players:", totalPlayers);
				if(temp == null)
					break;

				totalPlayers = Integer.parseInt(temp);
			} catch (NumberFormatException e) {}
		}
		while(liberalStupidAIPlayers + conservativeStupidAIPlayers + liberalMediumAIPlayers + conservativeMediumAIPlayers + liberalSmartAIPlayers + conservativeSmartAIPlayers  < 0 || 
			liberalStupidAIPlayers + conservativeStupidAIPlayers + liberalMediumAIPlayers + conservativeMediumAIPlayers + liberalSmartAIPlayers + conservativeSmartAIPlayers > 10)
		{
			try 
			{
				String temp = JOptionPane.showInputDialog("How many liberal stupid AI players?", liberalStupidAIPlayers);
				if(temp == null)
					break;

				liberalStupidAIPlayers = Integer.parseInt(temp);
			} catch (NumberFormatException e) {}

			try 
			{
				String temp = JOptionPane.showInputDialog("How many conservative stupid AI players?", conservativeStupidAIPlayers);
				if(temp == null)
					break;

				conservativeStupidAIPlayers = Integer.parseInt(temp);
			} catch (NumberFormatException e) {}

			try 
			{
				String temp = JOptionPane.showInputDialog("How many liberal medium AI players?", liberalMediumAIPlayers);
				if(temp == null)
					break;

				liberalMediumAIPlayers= Integer.parseInt(temp);
			} catch (NumberFormatException e) {}
			
			try 
			{
				String temp = JOptionPane.showInputDialog("How many conservative medium AI players?", conservativeMediumAIPlayers);
				if(temp == null)
					break;

				conservativeMediumAIPlayers= Integer.parseInt(temp);
			} catch (NumberFormatException e) {}
			
			try 
			{
				String temp = JOptionPane.showInputDialog("How many liberal smart AI players?", liberalSmartAIPlayers);
				if(temp == null)
					break;

				liberalSmartAIPlayers = Integer.parseInt(temp);
			} catch (NumberFormatException e) {}
			
			try 
			{
				String temp = JOptionPane.showInputDialog("How many conservative smart AI players?", conservativeSmartAIPlayers );
				if(temp == null)
					break;

				conservativeSmartAIPlayers = Integer.parseInt(temp);
			} catch (NumberFormatException e) {}
		}

		humanPlayers = totalPlayers - liberalStupidAIPlayers - conservativeStupidAIPlayers - liberalMediumAIPlayers - conservativeMediumAIPlayers - liberalSmartAIPlayers - conservativeSmartAIPlayers;				
				
		if(numberOfRoundsBeforeGUI == 0){	
			GameRound gameRound = new GameRound(createPlayers(totalPlayers, humanPlayers), true);
			gameRound.showGUI();
			gameRound.start();		
		}
		else{
			GameRound gameRound = new GameRound(createPlayers(totalPlayers, humanPlayers), false);
			while(numberOfRoundsBeforeGUI > 0)
			{
				gameRound.start();
				numberOfRoundsBeforeGUI--;
			}
			gameRound.showGUI();	
			gameRound.updateGUI();
			Printer.printEachPlayersTotalWins(players);
		}
	}

	public static void main(String args[]) 
	{		
		PokerManager pokerManager = new PokerManager();
		pokerManager.startNewGame();		
	}
}